package uns.fit.gis.ui;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Vector;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.control.VideoControl;

import uns.fit.gis.MapDrawer;
import uns.fit.gis.NokiaKeyDef;
import uns.fit.gis.Util;
import uns.fit.gis.fileio.FileWrapper;
import uns.fit.gis.fileio.PhotoData;

public class VideoCanvas2 extends Canvas implements CommandListener {
	private MapDrawer midlet;
	private Player player;
    private VideoControl videoControl;
    
	private Command backCommand, captureCommand;

	private String photoDescription;
	private int width, height;

	public VideoCanvas2(MapDrawer midlet) {
		width = getWidth();
		height = getHeight();
		this.midlet = midlet;

		backCommand = new Command("Back", Command.BACK, 2);
        captureCommand = new Command("Capture", Command.OK, 3);   
		addCommand(backCommand);
        addCommand(captureCommand);
        setCommandListener(this);

        showCamera();
        
		photoDescription = "encoding=jpeg&type=exif";
                //photoDescription = "encoding=jpeg&type=exif&width=800&height=600";
	}

	protected void paint(Graphics g) {
		g.setColor(255, 0, 0);
		g.drawRect(0, 0, width - 1, height - 1);
		g.drawRect(1, 1, width - 3, height - 3);
		
		g.setColor(0, 0, 255);
		g.fillRect(50, 50, 50, 50);
	}

	protected void keyPressed(int keyCode) {
		//midlet.showAlert(String.valueOf(keyCode));
		switch(keyCode) {
		case 53:
		case NokiaKeyDef.NOKIA_KEY_SELECT:
			new Thread() {
        		public void run() {
        			String dirPhotos = System.getProperty("fileconn.dir.photos");
		            if(dirPhotos == null) {
		            	dirPhotos = Util.STORAGE_FOLDER;
		            }
                                try {
                                byte[] photoData = videoControl.getSnapshot(photoDescription);



					StringBuffer strBuf = new StringBuffer();
					strBuf.append(dirPhotos);

					System.out.println(strBuf.toString());

					strBuf.append("GeoImage");
					/*
					Date currentDate = new Date();
					strBuf.append(currentDate);
					*/
					strBuf.append(System.currentTimeMillis());
					strBuf.append(".jpg");
					System.out.println(strBuf.toString());
					FileWrapper.save(new PhotoData(photoData, 0, 0), strBuf.toString());
					MapDrawer.showAlert("Saved to " + dirPhotos);
                                }
                                catch(MediaException me) {
                                    me.printStackTrace();
                                }

                    }
        		
        	}.start();
			break;
		default:
			break;
		}
	}
	
	public void commandAction(Command c, Displayable s){
        if (c == backCommand){
        	if(player != null)
        		if(player.getState() != Player.CLOSED)
        			player.close();
                videoControl = null;
                player = null;
                midlet.showMapCanvas();
        	
        } else if (c == captureCommand) {
            keyPressed(NokiaKeyDef.NOKIA_KEY_SELECT);
        }
    }
	
	public void showCamera(){
            try{
                try {
                    player = Manager.createPlayer("capture://image");
                }
                catch(Exception e) {
                    e.printStackTrace();
                    player = Manager.createPlayer("capture://video");
                }
                
                player.realize();

                videoControl = (VideoControl)player.getControl("VideoControl");
                player.start();

                videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, this);
                try {
                    videoControl.setDisplayLocation(2, 2);
                    videoControl.setDisplaySize(width - 4, height - 4);
                } catch (MediaException me) {
                    me.printStackTrace();
                }
                videoControl.setVisible(true);
            }
            catch (IOException ioe) {
                    ioe.printStackTrace();
            }
            catch (MediaException me) {
                    me.printStackTrace();
            }
        }

	public String getPhotoDescription() {
		return photoDescription;
	}

	public void setPhotoDescription(String photoDescription) {
		this.photoDescription = photoDescription;
	}
}
